home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / nehe9.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  5.2 KB  |  125 lines

  1.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2.     ' Allocate data
  3.     dim twinkle                         ' Twinkling stars
  4.     dim tp                        ' 'T' Key pressed?
  5.     
  6.     dim num: num = 50                ' Number of stars to draw
  7.     
  8.     ' Stars:
  9.     ' Basic4GL doesn't support structures.
  10.     ' We will use parallel arrays instead
  11.     struc SStar
  12.         dim r, g, b                ' Stars color
  13.         dim dist#                    ' Distance from centre
  14.         dim angle#                    ' Stars current angle
  15.     endstruc
  16.     dim SStar star(50), SStar &curStar
  17.     
  18.     dim zoom#: zoom# = -15.0            ' Viewing distance away from stars
  19.     dim tilt#: tilt# =  90            ' Tilt the view
  20.     dim spin#                        ' Spin twinkling stars
  21.     
  22.     dim loop                        ' General loop variable
  23.     dim key$                        ' General input variable
  24.     dim texture                    ' Storage for one texture
  25.     
  26.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  27.     ' Load bitmap
  28.     texture = LoadTexture ("Data\Star.bmp")
  29.     if texture = 0 then print "Failed to load bitmap": end: endif
  30.     
  31.     ' Make linear filtered texture
  32.     glBindTexture(GL_TEXTURE_2D, texture)
  33.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  34.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  35.     
  36.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  37.     ' Setup
  38.     
  39.     ' Setup OpenGL state
  40.     glBlendFunc(GL_SRC_ALPHA,GL_ONE)    ' Set the blending function for translucency
  41.     glEnable (GL_BLEND)                ' Enable blending
  42.     glEnable (GL_TEXTURE_2D)
  43.     glDisable (GL_DEPTH_TEST)
  44.     
  45.     ' Setup stars
  46.     for loop = 1 to num
  47.         &curStar = &star (loop)
  48.         curStar.angle# = 0                ' Start all the stars at angle zero
  49.         curStar.dist# = (loop * 5.0) / num    ' Calculate distance from the center
  50.         curStar.r = rnd () % 256            ' Give star[loop] A random red intensity
  51.         curStar.g = rnd () % 256            ' Give star[loop] A random green intensity
  52.         curStar.b = rnd () % 256            ' Give star[loop] A random blue intensity
  53.     next
  54.         
  55.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  56.     ' Main loop
  57.     while true
  58.  
  59.         ' Render frame
  60.     
  61.         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)    ' Clear the screen and the depth buffer
  62.         glBindTexture(GL_TEXTURE_2D, texture)                ' Select our texture
  63.  
  64.         for loop = 1 to num                            ' Loop through all the stars
  65.             &curStar = &star (loop)
  66.             glLoadIdentity()                        ' Reset the view before we draw each star
  67.             glTranslatef(0.0,0.0,zoom#)                       ' Zoom into the screen (Using the value in 'zoom')
  68.             glRotatef(tilt#,1.0,0.0,0.0)                    ' Tilt the view (Using the value in 'tilt')
  69.             glRotatef(curStar.angle#,0.0,1.0,0.0)         ' Rotate to the current stars angle
  70.             glTranslatef(curStar.dist#,0.0,0.0)            ' Move forward on the X plane
  71.             glRotatef(-curStar.angle#,0.0,1.0,0.0)        ' Cancel the current stars angle
  72.             glRotatef(-tilt#,1.0,0.0,0.0)                ' Cancel the screen tilt
  73.             
  74.             if twinkle then
  75.                 glColor4ub(star((num-loop)+1).r,star((num-loop)+1).g, star((num-loop)+1).b,255)
  76.                 glBegin(GL_QUADS)
  77.                     glTexCoord2f(0.0, 0.0): glVertex3f(-1.0,-1.0, 0.0)
  78.                     glTexCoord2f(1.0, 0.0): glVertex3f( 1.0,-1.0, 0.0)
  79.                     glTexCoord2f(1.0, 1.0): glVertex3f( 1.0, 1.0, 0.0)
  80.                     glTexCoord2f(0.0, 1.0): glVertex3f(-1.0, 1.0, 0.0)
  81.                 glEnd()
  82.             endif
  83.  
  84.             glRotatef(spin#,0.0,0.0,1.0)
  85.             glColor4ub(curStar.r,curStar.g,curStar.b,255)
  86.             glBegin(GL_QUADS)
  87.                 glTexCoord2f(0.0, 0.0): glVertex3f(-1.0,-1.0, 0.0)
  88.                 glTexCoord2f(1.0, 0.0): glVertex3f( 1.0,-1.0, 0.0)
  89.                 glTexCoord2f(1.0, 1.0): glVertex3f( 1.0, 1.0, 0.0)
  90.                 glTexCoord2f(0.0, 1.0): glVertex3f(-1.0, 1.0, 0.0)
  91.             glEnd()
  92.     
  93.             spin# = spin# + 0.01
  94.             curStar.angle# = curStar.angle# + (loop + 0.0)/num
  95.             curStar.dist# = curStar.dist# - 0.01
  96.             if curStar.dist# < 0.0 then
  97.                 curStar.dist# = curStar.dist# + 5.0
  98.                 curStar.r = rnd ()%256
  99.                 curStar.g = rnd ()%256
  100.                 curStar.b = rnd ()%256
  101.             endif
  102.         next
  103.  
  104.         SwapBuffers ()
  105.         
  106.  
  107.         ' User interface
  108.  
  109.         key$ = inkey$ ()
  110.         if key$ = "t" or key$ = "T" then
  111.             twinkle = not twinkle
  112.         endif
  113.         if ScanKeyDown (VK_UP) then
  114.             tilt# = tilt# - 0.5
  115.         endif
  116.         if ScanKeyDown (VK_DOWN) then
  117.             tilt# = tilt# + 0.5
  118.         endif
  119.         if ScanKeyDown (VK_PRIOR) then
  120.             zoom# = zoom# - 0.2
  121.         endif
  122.         if ScanKeyDown (VK_NEXT) then
  123.             zoom# = zoom# + 0.2
  124.         endif
  125.     wend